﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Algorithm.Data;

namespace AAA.Algorithm.Cluster.SOM
{
    public class SOMSimilarity : ISimilarity
    {
        public SOMSimilarity()
        {

        }

        #region ISimilarity Members
        public float Similarity(AAA.Algorithm.Data.INeuron sourceNeuron, AAA.Algorithm.Data.INeuron compareNeuron)
        {
            float fResult = float.NaN;
            try
            {
                float fSum = 0;
                for (int i = 0; i < sourceNeuron.FactorCount; i++) {
                    fSum = fSum + (float)Math.Pow(sourceNeuron.GetFactor(i) - compareNeuron.GetFactor(i), 2);
                }
                if (fSum != 0)
                {
                    fResult = 1 / (float)Math.Sqrt(fSum);
                }
                return fResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
            return fResult;
        }

        #endregion
    }
}
